package com.example.mytestdemo.view

import android.animation.ValueAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import com.example.mytestdemo.R
import com.example.mytestdemo.utils.dp


/**
 * 参考：
 *https://blog.csdn.net/u013147860/article/details/102876182
 */

class ScanView : androidx.appcompat.widget.AppCompatImageView {
    private var mPaint: Paint? = null
    private var width = 0f
    private var height = 0f

    val left = 0f
    var right = width
    val top = 0f
    var bottom = height
    var distanceLine = width / 6
    var mScanLineTop = -100f

    // 扫描线
    private val scanBitmap: Bitmap =
        BitmapFactory.decodeResource(resources, R.drawable.identify_ic_camera_scan_line)
    private val mScanLineRectF = RectF()

    private var anim: ValueAnimator? = null

    constructor(context: Context) : super(context) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init(context)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawCorner(canvas)
        drawScanLine(canvas)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        width = measuredWidth.toFloat()
        height = measuredHeight.toFloat()
        right = width
        bottom = height
        distanceLine = width / 6
        init(context)
    }

    private fun init(context: Context) {
        mPaint = Paint()
        mPaint!!.isAntiAlias = true
        mPaint!!.style = Paint.Style.STROKE
        mPaint!!.color = Color.GREEN
        mPaint!!.strokeWidth = 5f
        doAnimator()
    }

    private fun drawCorner(canvas: Canvas) {
        val path = Path()
        val left = 0f
        val right = width
        val top = 0f
        val bottom = height
        val distanceLine = width / 6

        //左上；
        path.moveTo(left, top + distanceLine)
        path.lineTo(left, top)
        path.lineTo(left + distanceLine, top)

        // 右上
        path.moveTo(right, top + distanceLine)
        path.lineTo(right, top)
        path.lineTo(right - distanceLine, top)

        // 右下
        path.moveTo(right - distanceLine, bottom)
        path.lineTo(right, bottom)
        path.lineTo(right, bottom - distanceLine)

        // 左下
        path.moveTo(left + distanceLine, bottom)
        path.lineTo(left, bottom)
        path.lineTo(left, bottom - distanceLine)

        canvas.drawPath(path, mPaint!!)
    }

    private fun drawScanLine(canvas: Canvas) {

        if (mScanLineTop + 100 >= height) {
            mScanLineRectF.set(
                left + paddingLeft,
                top + mScanLineTop + paddingTop,
                right - paddingRight,
                height - paddingBottom.toFloat()
            )
        } else if (mScanLineTop <= 0) {
            mScanLineRectF.set(
                left + paddingLeft,
                top + paddingTop,
                right - paddingRight,
                (mScanLineTop + 100 - paddingBottom).toFloat()
            )
        } else {
            mScanLineRectF.set(
                left + paddingLeft,
                top + mScanLineTop + paddingTop,
                right - paddingRight,
                (mScanLineTop + 100 - paddingBottom).toFloat()
            )
        }

        //绘制第一层,目标图像，在下层
        canvas.drawBitmap(
            scanBitmap,
            Rect(0, 0 + paddingTop, scanBitmap.width, scanBitmap.height - paddingBottom),
            mScanLineRectF,
            mPaint
        )

//        moveScanLine()
    }

    /**
     * 移动扫描线
     */
    private fun moveScanLine() {
        mScanLineTop += 2.dp
        if (mScanLineTop > scanBitmap.height) {
            mScanLineTop = 0f
        }
        postInvalidateDelayed(4, 0, 0, measuredWidth, measuredHeight)
    }

    private fun doAnimator() {
        anim = ValueAnimator()
        anim = ValueAnimator.ofFloat(-100f, height - paddingBottom)
        anim?.let {
            anim?.repeatCount = ValueAnimator.INFINITE //设置无限重复

//            anim?.repeatMode = ValueAnimator.REVERSE //设置重复模式

            anim?.duration = 5000
            anim?.addUpdateListener(animatorUpdateListener)
            anim?.start()
        }

    }


    private val animatorUpdateListener: AnimatorUpdateListener =
        AnimatorUpdateListener { animation ->
            mScanLineTop = animation.animatedValue as Float
            invalidate()
        }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        anim?.removeUpdateListener(animatorUpdateListener)
        anim?.cancel()
    }
}